import axios from 'axios'
import Vue from 'vue'

// const RNTY_BASE_URL = 'http://www.mxzhzl.com/'
// const API_RNTY_URL = 'http://192.168.3.13:9001/'
// const API_ACCOUNT_RNTY_URL = 'http://192.168.3.13:9000/'
// const API_SHOP_RNTY_URL = 'http://192.168.3.15:8080/'
const API_RNTY_URL = 'http://api.ruinatiyu.com/'
const API_SHOP_RNTY_URL = 'http://shop2.jyingying.club/'
const API_ACCOUNT_RNTY_URL = 'http://api.account.ruinatiyu.com/'
// const SHOP_RNTY_URL = 'http://192.168.3.51:8080/'
// const RNTY_ACCOUNT_URL = 'http://192.168.3.51:8001/'
const SHOP_RNTY_URL = 'http://shop.ruinatiyu.com/'
const RNTY_ACCOUNT_URL = 'http://account.ruinatiyu.com/'
const CLIENT_ID = 4
const CLIENT_SECRET = 'L60f0vTWC-7labXVGDoOj2HVns-YqkbO'
const _this = Vue.prototype

function authLogin(methods="login") {
  request({
    method: 'post',
    baseURL: API_ACCOUNT_RNTY_URL,
    url: 'clients/authentication?methods=' + methods,
    data: {
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
      redirect_url: window.location.href
    }
  }).then(({data: {auth_code, login_url}}) => {
    console.log(auth_code)
    _this.$cookie.set('auth_code', auth_code, { domain: '.ruinatiyu.com'})
    window.location.href = login_url;
    return false
  })
}

function tokenRequest({method = 'get', baseURL, url, param, data}) {
  let token = _this.$cookie.get('rnty_access_token')
  if (!token) {
    authLogin()
    return
  }
  axios.defaults.baseURL = baseURL
  axios.defaults.transformRequest = [function (data) {
    // Do whatever you want to transform the data
    let ret = ''
    for (let it in data) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    }
    return ret
  }]
  // axios.defaults.withCredentials = true
  axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  axios.defaults.headers.common['Authorization'] = `Bearer ${token}`
  return axios({
    method: method,
    url: url,
    params: param,
    data:data
  }).catch((error) => {
    if (error.response.status === 401) {
      authLogin()
    }
  })
}

function request({method = 'get', baseURL, url, param = null, data = null}) {
  axios.defaults.baseURL = baseURL
  axios.defaults.transformRequest = [function (data) {
    // Do whatever you want to transform the data
    let ret = ''
    for (let it in data) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    }
    return ret
  }]
  axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

  return axios({
    method: method,
    url: url,
    params: param,
    data: data
  })
}

  //时间戳转换时间
  function timestampToTime(timestamp){
    function format(n) {
      return n < 10? '0' + n : n;
    }
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-';
    let M = format((date.getMonth())) + '-';
    let D = format(date.getDate()) + ' ';
    let h = format(date.getHours()) + ':';
    let m = format(date.getMinutes()) + ':';
    let s = format(date.getSeconds());
    return Y+M+D+h+m+s;
  }

export default {
  API_RNTY_URL,
  API_SHOP_RNTY_URL,
  API_ACCOUNT_RNTY_URL,
  SHOP_RNTY_URL,
  RNTY_ACCOUNT_URL,
  CLIENT_ID,
  CLIENT_SECRET,
  tokenRequest,
  request,
  authLogin,
  timestampToTime
}
